<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .container {
            display: flex;
            flex-direction: row;
            align-items: center;

        }

        .item {
            width: 60px;
            height: 30px;
            border: 1px solid black;
            margin-left: 10px;
        }
    </style>
</head>

<body>
    <div class="container">
        <h2>点击切换颜色</h2>
        <div class="item"></div>
        <div class="item"></div>
        <div class="item"></div>
    </div>

    <script>
        const item = document.querySelectorAll('.item')


        //变量i互不影响 所以let 可以 但是var不行
        for (let i = 0; i < item.length; i++) {
            item[i].onclick = () => {
                item[i].style.background = 'pink'
            }
        }

        console.log(window.i);

    </script>
</body>

</html>